package org.colafries.admin.config;

import com.alibaba.druid.sql.parser.ParserException;
import org.colafries.admin.exceptions.ServiceException;
import org.colafries.admin.wrapper.Response;
import org.colafries.admin.wrapper.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author colafries
 * @version v1.0
 * @ClassName ControllerAdvice
 * @Description 统一异常处理
 * @date Created in 2024/3/20
 **/
@RestControllerAdvice
@Slf4j
public class ControllerAdvice {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Response<?> handleValidException(MethodArgumentNotValidException e) {
        log.error("数据校验出现问题(MethodArgumentNotValidException):{},异常类型:{}", e.getMessage(), e.getClass());
        Map<String, String> map = new HashMap<>((int) (10 / 0.75 + 1));
        BindingResult bindingResult = e.getBindingResult();
        bindingResult.getFieldErrors().forEach((fieldError) ->
                map.put(fieldError.getField(), fieldError.getDefaultMessage()));
        return ResponseWrapper.resp("909100", "参数异常", map);
    }

    /***
     * sql执行异常
     * @param e
     * @author colafries
     * @Date 2024/3/24
     */
    @ExceptionHandler(value = UncategorizedSQLException.class)
    public Response<?> handleException(UncategorizedSQLException e) {
        log.warn("SQL解析校验错误信息(UncategorizedSQLException)：{}", e.getMessage());
        return ResponseWrapper.resp("731009", e.getMessage());
    }

    /***
     * sql执行异常
     * @param e
     * @author colafries
     * @Date 2024/3/24
     */
    @ExceptionHandler(value = SQLException.class)
    public Response<?> handleException(SQLException e) {
        log.warn("SQL解析校验错误信息(SQLException)：{}", e.getMessage());
        return ResponseWrapper.resp("731009", e.getMessage());
    }

    /**
     * sql执行异常
     *
     * @param e
     * @author colafries
     * @Date 2024/3/24
     */
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public Response<?> handleException(BadSqlGrammarException e) {
        log.warn("SQL解析校验错误信息(BadSqlGrammarException)：{}", e.getMessage());
        return ResponseWrapper.resp("731009", e.getMessage());
    }

    /**
     * sql 解析异常
     *
     * @param e
     * @author colafries
     * @Date 2024/3/24
     */
    @ExceptionHandler(value = ParserException.class)
    public Response<?> handleException(ParserException e) {
        log.warn("SQL解析校验错误信息(ParserException)：", e);
        return ResponseWrapper.resp("731009", e.getMessage());
    }


    /**
     * 服务异常处理
     *
     * @param e
     * @author colafries
     * @Date 2024/3/20
     */
    @ExceptionHandler(value = ServiceException.class)
    public Response<?> handleException(ServiceException e) {
        log.error("错误码：{}，错误信息：{} ", e.getCode(), e.getMessage());
        return ResponseWrapper.resp(e.getCode(), e.getMessage());
    }

    /**
     * 其他异常处理
     *
     * @param e
     * @author colafries
     * @Date 2024/3/20
     */
    @ExceptionHandler(value = Exception.class)
    public Response<?> handleException(Exception e) {
        log.error("错误信息：{}", e.getMessage());
        return ResponseWrapper.error("未知异常，请联系管理员：" + e.getMessage());
    }
}
